package common.inter;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

import model.ServicerCompany;
import model.Station;
import model.Wxuser;

public class ServicerStationInter implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
		Controller con = inv.getController() ;
		Wxuser wxuser = con.getAttr("wxuser");
		Long id = con.getParaToLong("id");
		if (null == id) {
			id = con.getParaToLong("stationId");
		}
		if (null == id) {
			con.renderJson(Ret.fail("err","参数错误"));
			return ;
		}
		Station station = Station.dao.findById(id);
		if (null == station) {
			con.renderJson(Ret.fail("err","站点信息不存在"));
			return ;
		}
		ServicerCompany servicerCompany = ServicerCompany.dao.findFirstByServiserIdAndCompanyId(wxuser.getCompanyId(), station.getCompanyId());
		if (null == servicerCompany && !wxuser.isMangerRole() && !station.getCompanyId().equals(wxuser.getCompanyId() )) {
			con.renderJson(Ret.fail("err","非运维企业禁止操作"));
			return ;
		}
		con.set("station", station);
		con.set("servicerCompany", servicerCompany);
		inv.invoke();
		con.removeAttr("station");
		con.removeAttr("servicerCompany");
	}

}
